<?php

namespace App\Models;

use Eloquent as Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * Class Questions
 * @package App\Models
 * @version July 23, 2018, 3:16 am UTC
 *
 * @property string code
 * @property string content
 * @property integer next_id
 * @property integer temp_id
 * @property boolean type
 */
class Questions extends \Illuminate\Database\Eloquent\Model
{
    use SoftDeletes;

    public $table = 'questions';
    
    const CREATED_AT = 'created_at';
    const UPDATED_AT = 'updated_at';

    const WAIT_CHECK = 0;
    const SIMPLE_CHECK = 1;
    const MULTI_CHECK = 2;
    const SHORT_ANSWER = 3;

    protected $dates = ['deleted_at'];


    public $fillable = [
        'code',
        'content',
        'next_id',
        'temp_id',
        'type'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'code' => 'string',
        'content' => 'string',
        'id' => 'integer',
        'next_id' => 'integer',
        'temp_id' => 'integer',
        'type' => 'integer'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'code' => 'required',
	    'content' => 'required',
	    'temp_id' => 'required'
    ];

    private static $type = [
    	self::SIMPLE_CHECK => '单选',
    	self::MULTI_CHECK => '多选',
    	self::SHORT_ANSWER => '简答'
    ];

    public static function getType() {
    	return self::$type;
    }

    public function temp() {
    	return $this->belongsTo(Temp::class, 'temp_id', 'id');
    }

    public function options() {
    	return $this->hasMany(Options::class, 'question_id', 'id');
    }

    public function nextQuestion() {
    	return $this->hasOne(self::class, 'id', 'next_id');
    }
}
